import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;

/**
 * Created by xuwei on 2014/11/10.
 */
public class WsServer {

    @WebSocket
    public static class EchoSocket{
        public void onMessage(Session session,String message){
            //async
            session.getRemote().sendStringByFuture(message);
        }
    }

    public static class EchoServlet extends WebSocketServlet{

        @Override
        public void configure(WebSocketServletFactory factory) {
            factory.register(EchoSocket.class);
        }
    }

    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);

        context.addServlet(new ServletHolder(EchoServlet.class),"/echo");
        server.start();
        context.dumpStdErr();
        server.join();
    }
}
